package com.biqee.util;

import java.util.Collection;
import java.util.Date;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.JSONUtils;

public class JSONUtil {
    private static JsonConfig  DEFAULT_JSON_CONFIG =null;
    
//    public static JsonConfig AJAX_JSON_CONFIG = null;
    static {
//        AJAX_JSON_CONFIG =  new JsonConfig();
//        AJAX_JSON_CONFIG.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl());
//        
        DEFAULT_JSON_CONFIG =  new JsonConfig();
        DEFAULT_JSON_CONFIG.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl());
        DEFAULT_JSON_CONFIG.registerJsonValueProcessor(String.class, new JsonUnicodeValueProcessorImpl() );
    }
   
    
//    /**
//     * 对JSON每一个KEY的VALUE文本中可能引起问题的特殊字符串做JS转义 双引号,单引号都转义
//     * 
//     * @param str
//     * @return
//     */
//    public static String escapeValue(String json) {
//        if (json == null || json.length() <= 0)
//            return json;
//
//        json = json.replaceAll("\\\\", "\\\\\\\\");
//        json = json.replaceAll("\"", "\\\\\"");
//        json = json.replaceAll("'", "\\\\'");
//        json = json.replaceAll("\b", "\\\\b");
//        json = json.replaceAll("\n", "\\\\n");
//        json = json.replaceAll("\t", "\\\\t");
//        json = json.replaceAll("\f", "\\\\f");
//        json = json.replaceAll("\r", "\\\\r");
//
//        return json;
//    }




    /**
     * 将bean转换为jsonString ,支持标准javabean,Collection,Object[]
     * 如果需要结果给javacript 使用 需要使用toJSONString(object,JSONUtil.AJAX_JSON_CONFIG);
     * @param obj
     * @return
     */
    public static String toJSONString(Object obj) {
        return toJSONString(obj,DEFAULT_JSON_CONFIG);
    }
    /**
     * 将bean 转换为jsonString,需要指定参数JsonConfig
     * @param obj
     * @param jsonConfig
     * @author binda.mabd  
     * @return
     */
    @SuppressWarnings({"static-access" })
	public static String toJSONString(Object obj,  JsonConfig  jsonConfig ) {
        if(obj==null){
            return null;
        }
        String result = null ;
         if(obj instanceof String) {
        	 return (String)obj;
         } else if (obj instanceof Collection) {
            JSONArray jsons = new JSONArray();
            jsons.addAll((Collection) obj,jsonConfig);
            result = jsons.toString();
        } else if(JSONUtils.isArray(obj)){
            JSONArray jsons = new JSONArray();
            jsons.add(obj,jsonConfig);
            result = jsons.toString();
        }  else {
            JSONObject json = new JSONObject();
            result = json.fromObject(obj,jsonConfig).toString();
        }
         
         return result ;
         
    }
    
    /**
     * 将json 对象转为javabean
     * @param json
     * @param beanClass
     * @author binda.mabd  
     * @return
     */
	public static Object jsonObjectToBean(JSONObject json,Class beanClass){
        
        return JSONObject.toBean(json,beanClass );
       
    }
    
    
    /**
     * 将json String 对象转为javabean
     * @param json
     * @param beanClass
     * @author binda.mabd 
     * @return
     */
	public static Object jsonStringObjectToBean(String jsonString,Class beanClass){
        
        return JSONObject.toBean(JSONObject.fromObject(jsonString),beanClass );
       
    }
    /**
	 * 将 jsonString 代表的字符串转换为对象
	 * @param jsonString
	 * @return
	 */
	public static Object jsonToObject(String jsonString) {
		if(jsonString == null) {
			return null;
		}
		
		JSONObject jsonObject = JSONObject.fromObject(jsonString);  
		return JSONObject.toBean(jsonObject);
	}
    
    
    

}


 class JsonValueProcessorImpl implements JsonValueProcessor {
    /* (non-Javadoc)
     * @see net.sf.json.processors.JsonValueProcessor#processArrayValue(java.lang.Object, net.sf.json.JsonConfig)
     */
    public Object processArrayValue(Object value, JsonConfig arg1) {
        String[] obj={};
        if(value instanceof Date[]){
         Date[] dates=(Date[])value;
         obj =new String[dates.length];
         for (int i = 0; i < dates.length; i++) {
          obj[i]=DateUtil.format(dates[i]);
         }
        }
        return obj;

    }

    /* (non-Javadoc)
     * @see net.sf.json.processors.JsonValueProcessor#processObjectValue(java.lang.String, java.lang.Object, net.sf.json.JsonConfig)
     */
    public Object processObjectValue(String key, Object value, JsonConfig arg2) {
        if(value instanceof Date){
            String str=DateUtil.format((Date)value);
            return str;
           }
           return value;
    }

}
 
 
 class JsonUnicodeValueProcessorImpl implements JsonValueProcessor {
	    /* (non-Javadoc)
	     * @see net.sf.json.processors.JsonValueProcessor#processArrayValue(java.lang.Object, net.sf.json.JsonConfig)
	     */
	    public Object processArrayValue(Object value, JsonConfig arg1) {
	        String[] obj={};
	        if(value instanceof String[]){
	         String[] strs=(String[])value;
	         obj =new String[strs.length];
	         for (int i = 0; i < strs.length; i++) {
	          obj[i]=UnicodeTransUtils.toEncodedUnicode(strs[i], false);
	         }
	        }
	        return obj;

	    }

	    /* (non-Javadoc)
	     * @see net.sf.json.processors.JsonValueProcessor#processObjectValue(java.lang.String, java.lang.Object, net.sf.json.JsonConfig)
	     */
	    public Object processObjectValue(String key, Object value, JsonConfig arg2) {
	        if(value instanceof String){
	            String str=UnicodeTransUtils.toEncodedUnicode((String)value, false);
	            return str;
	           }
	           return value;
	    }

	}